<?php

namespace app\common\model;

use think\Model;
use traits\model\SoftDelete;
use app\common\model\GoodsCategory;
class Goods extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'goods';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
    ];

    public function getThumbnailImageAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['thumbnailimage']) ? $data['thumbnailimage'] : '');
        return get_app_image_url($value);
    }
    public function getGoodsContentAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['goodscontent']) ? $data['goodscontent'] : '');
        return imageUrl($value);
    }
    public function getThumbnailImagesAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['thumbnailimages']) ? $data['thumbnailimages'] : '');
        $value=explode(',',$value);
        foreach ($value as $k=>&$v){
            $v=get_app_image_url($v);
        }
        return $value;
    }
    public function getImagesAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['images']) ? $data['images'] : '');
        $value=explode(',',$value);
        foreach ($value as $k=>&$v){
            $v=get_app_image_url($v);
        }
        return $value;
    }
    public function category1()
    {
        return $this->belongsTo('GoodsCategory', 'cid1', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function category2(){
        return $this->belongsTo('GoodsCategory', 'cid2', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function category3(){
        return $this->belongsTo('GoodsCategory', 'category_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function admin()
    {
        return $this->belongsTo('ShopApply', 'shop_id', 'id', [], 'LEFT')
            ->setEagerlyType(0);
    }

    /**
     * 商品列表
     * */
    public static function getList($where=[],$order='id desc'){
        $where['goods.status']='1';
        $where['goods.state']='1';
        $list = self::where($where)
            ->with('admin')
            ->order($order)
//            ->fetchSql()
            ->paginate();
//        dd($list);
        foreach ($list as $k=>$v){
            $v->getRelation('admin')->visible(['company_name','logo','city1']);
            $v['starttime'] = date('Y-d-d H:i:s',$v['starttime']);
            $v['endtime'] = date('Y-d-d H:i:s',$v['endtime']);
            $v['createtime'] = date('Y-d-d H:i:s',$v['createtime']);
        }
        return $list;
    }
    /**
     * 商品详情
     * */
    public static function getInfo($id,$user_id=''){


        $where['id'] = $id;
        $list = self::where($where)
            ->with(['admin'])
            ->find();
        $list->getRelation('admin')->visible(['company_name','logo','city1','city']);
        $whe['typeid'] = $list['id'];
        $whe['type'] = 1;
        $whe['status'] = 1;
        $whe['state'] = 2;
        if(!empty($user_id)){  $whe['uid'] = $user_id; }
        //收藏
        $list['user_collect'] = UserCollect::where($whe)
            ->count();
        //捐增人信息
        $User = User::where(['id'=>$list['admin']['user_id']])->find();
        $list['user'] = $User;

    }
    /**
     * 根据商户id 获取商品信息
     * */
    public static function getShopList($where=[],$order='id desc',$field='*'){
        $where['status']='1';
        $where['state']='1';
        $list = self::where($where)
            ->field($field)
            ->order($order)
            ->paginate();
        $whe['type'] ='1';
        $whe['status'] ='1';
        $whe['state'] ='2';
        foreach ($list as $k=>$v){
            $whe['typeid'] = $v['id'];
            $v['UserCollect'] = UserCollect::where($whe)->count();
        }
//        $list['count'] = self::where($where)
//            ->field($field)
////            ->order($order)
//            ->count();
        return $list;
    }

}
